using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Chat
{
	/// <summary>
	/// Summary description for FormMessage.
	/// </summary>
	public class FormMessage : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.RichTextBox rtbConversation;
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.TextBox txtMessage;
		private System.Windows.Forms.Timer tmrBuzz;
		private System.Windows.Forms.Timer tmrBuzzOff;

		internal string contact;
		private System.Windows.Forms.Timer tmrBuzzCount;

		private int buzzCount=0;

		public FormMessage()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMessage));
			this.rtbConversation = new System.Windows.Forms.RichTextBox();
			this.btnSend = new System.Windows.Forms.Button();
			this.txtMessage = new System.Windows.Forms.TextBox();
			this.tmrBuzz = new System.Windows.Forms.Timer(this.components);
			this.tmrBuzzOff = new System.Windows.Forms.Timer(this.components);
			this.tmrBuzzCount = new System.Windows.Forms.Timer(this.components);
			this.SuspendLayout();
			// 
			// rtbConversation
			// 
			this.rtbConversation.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.rtbConversation.Location = new System.Drawing.Point(8, 8);
			this.rtbConversation.Name = "rtbConversation";
			this.rtbConversation.ReadOnly = true;
			this.rtbConversation.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
			this.rtbConversation.Size = new System.Drawing.Size(272, 104);
			this.rtbConversation.TabIndex = 0;
			this.rtbConversation.Text = "";
			// 
			// btnSend
			// 
			this.btnSend.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnSend.Location = new System.Drawing.Point(216, 120);
			this.btnSend.Name = "btnSend";
			this.btnSend.Size = new System.Drawing.Size(64, 40);
			this.btnSend.TabIndex = 1;
			this.btnSend.Text = "Send";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// txtMessage
			// 
			this.txtMessage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtMessage.Location = new System.Drawing.Point(8, 120);
			this.txtMessage.Multiline = true;
			this.txtMessage.Name = "txtMessage";
			this.txtMessage.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txtMessage.Size = new System.Drawing.Size(200, 40);
			this.txtMessage.TabIndex = 2;
			this.txtMessage.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtMessage_KeyDown);
			this.txtMessage.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtMessage_KeyUp);
			// 
			// tmrBuzz
			// 
			this.tmrBuzz.Interval = 31;
			this.tmrBuzz.Tick += new System.EventHandler(this.tmrBuzz_Tick);
			// 
			// tmrBuzzOff
			// 
			this.tmrBuzzOff.Interval = 1000;
			this.tmrBuzzOff.Tick += new System.EventHandler(this.tmrBuzzOff_Tick);
			// 
			// tmrBuzzCount
			// 
			this.tmrBuzzCount.Enabled = true;
			this.tmrBuzzCount.Interval = 5000;
			this.tmrBuzzCount.Tick += new System.EventHandler(this.tmrBuzzCount_Tick);
			// 
			// FormMessage
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(291, 170);
			this.Controls.Add(this.txtMessage);
			this.Controls.Add(this.btnSend);
			this.Controls.Add(this.rtbConversation);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MinimumSize = new System.Drawing.Size(296, 200);
			this.Name = "FormMessage";
			this.Text = "Instant Message";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.FormMessage_Closing);
			this.Resize += new System.EventHandler(this.FormMessage_Resize);
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		internal void AddText(string person,string message)
		{
			rtbConversation.SelectionFont=new System.Drawing.Font("Microsoft Sans Serif",8.25f,System.Drawing.FontStyle.Bold);
			rtbConversation.AppendText(person+" : ");
			rtbConversation.SelectionFont=new System.Drawing.Font("Microsoft Sans Serif",8.25f,System.Drawing.FontStyle.Regular);
			rtbConversation.AppendText(message+" \n");
			txtMessage.Focus();
		}
		private void btnSend_Click(object sender, System.EventArgs e)
		{
			rtbConversation.SelectionFont=new System.Drawing.Font("Microsoft Sans Serif",8.25f,System.Drawing.FontStyle.Bold);
			rtbConversation.AppendText(Global.username+" : ");
			rtbConversation.SelectionFont=new System.Drawing.Font("Microsoft Sans Serif",8.25f,System.Drawing.FontStyle.Regular);
			rtbConversation.AppendText(txtMessage.Text+" \n");
			rtbConversation.Focus();

			Global.server.Send(Global.username,contact,txtMessage.Text);

			txtMessage.Focus();
			txtMessage.Clear();
		}

		private void txtMessage_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Enter)
			{
				btnSend_Click(null,null);		
			}
			if(e.KeyCode==Keys.G&&e.Control)
			{
				if(buzzCount>=2)
				{
					MessageBox.Show("You can buzz the same contact more than twice in five seconds.");
					return;
				}
				buzzCount++;
				Global.server.Send(Global.username,contact,"BUZZ BHI KARO");
				rtbConversation.SelectionFont=new System.Drawing.Font("Microsoft Sans Serif",8.25f,System.Drawing.FontStyle.Bold);
				rtbConversation.AppendText(Global.username+" : BUZZ \n");
				tmrBuzz.Enabled=true;
				tmrBuzzOff.Enabled=true;
				txtMessage.Focus();			
			}
		}

		private void FormMessage_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			Global.windowList.Remove(contact);
		}

		private void txtMessage_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Enter)
			{
				txtMessage.Clear();
			}
		}

		private void FormMessage_Resize(object sender, System.EventArgs e)
		{
			rtbConversation.Width=this.Width-24;
			rtbConversation.Height=this.Height-96;
			btnSend.Location=new System.Drawing.Point(this.Width-80,this.Height-80);

			txtMessage.Location=new Point(8,this.Height-80);
			txtMessage.Size =new System.Drawing.Size(this.Width-96,40);
		}

		int loc=0;
		private void tmrBuzz_Tick(object sender, System.EventArgs e)
		{
			switch(loc)
			{
				case 0:
					this.Location=new Point(this.Location.X+10,this.Location.Y);
					break;
				case 1:
					this.Location=new Point(this.Location.X,this.Location.Y+10);
					break;
				case 2:
					this.Location=new Point(this.Location.X-10,this.Location.Y);
					break;
				case 3:
					this.Location=new Point(this.Location.X,this.Location.Y-10);
					break;
			}
			loc++;
			loc%=4;
		}

		private void tmrBuzzOff_Tick(object sender, System.EventArgs e)
		{
			tmrBuzz.Enabled=false;
			tmrBuzz.Enabled=false;
		}

		private void tmrBuzzCount_Tick(object sender, System.EventArgs e)
		{
			buzzCount=0;
		}
	}
}
